package com.example.demo.service.annot;

import com.example.demo.annotation.SkuServCode;
import com.example.demo.entity.OrderDataReq;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName OrderDataService
 * @Author bsy
 * @Description 实现数据处理业务类映射
 * @Date 2023/2/13 21:56
 * @Version 1.0
 */
@Service
public class OrderDataService implements ApplicationListener<ContextRefreshedEvent> {

    private static Map<String, IOrderDataService> orderDataMap = null;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ApplicationContext applicationContext = event.getApplicationContext();
        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(SkuServCode.class);
        if (beansWithAnnotation != null) {
            orderDataMap = new HashMap<>();
            beansWithAnnotation.forEach((key, value) -> {
                String servType = value.getClass().getAnnotation(SkuServCode.class).value();
                orderDataMap.put(servType, (IOrderDataService) value);
            });
        }
    }

    public void orderDataHandle(OrderDataReq orderDataReq) {
        orderDataMap.get(orderDataReq.getSkuNum()).orderDataHandle(orderDataReq);
    }
}
